package com.sailfish.springbootdemo.controller.db2;

import com.sailfish.springbootdemo.pojo.Result;
import com.sailfish.springbootdemo.pojo.db2.PcbSpecialProcessInfo;
import com.sailfish.springbootdemo.pojo.db2.PcbSpecialProcessVersion;
import com.sailfish.springbootdemo.service.db2.PcbSpecialProcessService;
import com.sailfish.springbootdemo.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.*;

@RestController
@CrossOrigin
@RequestMapping("/pcbSpecialProcess")
public class PcbSpecialProcessController {

    @Autowired
    private PcbSpecialProcessService pcbSpecialProcessService;

    @PostMapping("/getAllPcbSpecialProcess")//, Integer page,Integer pageSize
    public Result getAllPcbSpecialProcess(String pcbSpecialProcessName, String pcbSpecialProcessNameEn, String version, Integer status) {
        List<PcbSpecialProcessInfo> allData = pcbSpecialProcessService.getAllByLimit(pcbSpecialProcessName, pcbSpecialProcessNameEn, version, status);
        return ResultUtil.success(allData);
    }

    @PostMapping("/addPcbSpecialProcessInfo")
    public Result addPcbSpecialProcessInfo(String pcbSpecialProcessName, String pcbSpecialProcessNameEn, String version,
                                           String remark, Integer sort, Integer status, String changeHistory) {
        if (pcbSpecialProcessName == null) {
            return ResultUtil.error(500, "参数pcbSpecialProcessName为空", "", null);
        }
        if (pcbSpecialProcessNameEn == null) {
            return ResultUtil.error(500, "参数pcbSpecialProcessNameEn为空", "", null);
        }
        PcbSpecialProcessInfo param = new PcbSpecialProcessInfo();
        param.setPcbSpecialProcessName(pcbSpecialProcessName);
        param.setPcbSpecialProcessNameEn(pcbSpecialProcessNameEn);
        param.setVersion(version);
        param.setCreateTime(new Date());
        param.setSort(sort);
        param.setStatus(status);
        param.setRemark(remark);
        param.setChangeHistory(changeHistory);
        return pcbSpecialProcessService.insert(param);
    }

    @PostMapping("/updatePcbSpecialProcessInfo")
    public Result updatePcbSpecialProcessInfo(Integer pcbSpecialProcessId, String pcbSpecialProcessName, String remark,
                                              String pcbSpecialProcessNameEn, String version, Integer sort, Integer status, String changeHistory) {
        if (pcbSpecialProcessId == null) {
            return ResultUtil.error(500, "参数pcbSpecialProcessId为空", "param pcbSpecialProcessId is null", null);
        }
        if (pcbSpecialProcessName == null) {
            return ResultUtil.error(500, "参数pcbSpecialProcessName为空", "param pcbSpecialProcessName is null", null);
        }
        if (pcbSpecialProcessNameEn == null) {
            return ResultUtil.error(500, "参数pcbSpecialProcessNameEn为空", "param pcbSpecialProcessNameEn is null", null);
        }
        PcbSpecialProcessInfo param = new PcbSpecialProcessInfo();
        param.setPcbSpecialProcessName(pcbSpecialProcessName);
        param.setPcbSpecialProcessNameEn(pcbSpecialProcessNameEn);
        param.setVersion(version);
        param.setUpdateTime(new Date());
        param.setPcbSpecialProcessId(pcbSpecialProcessId);
        param.setSort(sort);
        param.setStatus(status);
        param.setRemark(remark);
        param.setChangeHistory(changeHistory);
        return pcbSpecialProcessService.update(param);
    }

    @PostMapping("/deletePcbSpecialProcessInfo")
    public Result deletePcbSpecialProcessInfo(Integer id) {
        if (id == null) {
            return ResultUtil.error(500, "参数id为空", "", null);
        }
        return pcbSpecialProcessService.delete(id);
    }

    @PostMapping("/insertOrUpdatePcbSpecialProcessData")
    public Result insertOrUpdatePcbSpecialProcessData(@RequestBody String data) {
        return pcbSpecialProcessService.insertOrUpdatePcbSpecialProcessData(data);
    }

    @PostMapping("/getPcbSpecialProcessData")
    public Result getPcbSpecialProcessData(String version) {
        List<PcbSpecialProcessInfo> pcbSpecialProcessData = pcbSpecialProcessService.getPcbSpecialProcessData(null, null, version, null);
        return ResultUtil.success(pcbSpecialProcessData);
    }

    @PostMapping("/getCurrentVersion")
    public Result getCurrentVersion() {
        String v = pcbSpecialProcessService.getCurrentVersion();
        return ResultUtil.success(v);
    }

    @PostMapping("/upgrade")
    public Result upgrade(String version) {
        return pcbSpecialProcessService.upgrade(version);
    }

    @PostMapping("/getVersionList")
    public Result getVersionList() {
        List<PcbSpecialProcessVersion> versionList = pcbSpecialProcessService.getVersionList();
        return ResultUtil.success(versionList);
    }

    @PostMapping("/import")
    public Result importFile(HttpServletRequest req) {
        return pcbSpecialProcessService.readExcel(req);
    }

    /*
    导出特殊工艺参数
     */
    @PostMapping("/outputSpecialProcessParam")
    public Result outputSpecialProcessParam(String version) {
        Result result = pcbSpecialProcessService.outputSpecialProcessParam(null, null, version);
        if (result.getCode().equals(200)) {
            try {
                String filepath = (String) result.getData();
                File file = new File(filepath);
                String fname = file.getName();
                byte[] bytedata = null;
                InputStream in = new FileInputStream(file);
                bytedata = new byte[in.available()];
                in.read(bytedata);
                in.close();
                Base64.Encoder encoder = Base64.getEncoder();
                String base64 = encoder.encodeToString(bytedata);

                Map<String, Object> map = new HashMap<>();
                map.put("base64", base64);
                map.put("fileName", fname);
                return ResultUtil.success(map);
            } catch (Exception e) {
                e.printStackTrace();
                return ResultUtil.error(500, e.getMessage(), e.getMessage(), null);
            }
        } else {
            return result;
        }
    }

    /*
    导出特殊工艺数据
     */
    @PostMapping("/outputSpecialProcessData")
    public Result outputSpecialProcessData(String version) {
        Result result = pcbSpecialProcessService.outputSpecialProcessData(null, null, version);
        if (result.getCode().equals(200)) {
            try {
                String filepath = (String) result.getData();
                File file = new File(filepath);
                String fname = file.getName();
                byte[] bytedata = null;
                InputStream in = new FileInputStream(file);
                bytedata = new byte[in.available()];
                in.read(bytedata);
                in.close();
                Base64.Encoder encoder = Base64.getEncoder();
                String base64 = encoder.encodeToString(bytedata);

                Map<String, Object> map = new HashMap<>();
                map.put("base64", base64);
                map.put("fileName", fname);
                return ResultUtil.success(map);
            } catch (Exception e) {
                e.printStackTrace();
                return ResultUtil.error(500, e.getMessage(), e.getMessage(), null);
            }
        } else {
            return result;
        }
    }
}
